import util from '../assets/js/MethodsUtil'
import jQuery from "jquery"
export default {
    install(Vue, options) {
        if(jQuery){
            jQuery(document).bind("ajaxSend", function(event, request, settings){
                var token = util.getcookie("token");
                if(token){
                    var headers = settings.headers || {};
                    headers["Authorization"] = token;
                    request.setRequestHeader("Authorization", token);
                    settings.headers = headers;
                }
                request.setRequestHeader("X-Requested-With", 'XMLHttpRequest');
                settings.headers = headers;
            }).bind("ajaxComplete", function(event, xhr, settings){
                if(xhr.status == 401 && xhr.responseText){
                    console.log("未登录！");
                    util.remove("token");
                    let host = window.location.host;
                    let protocal = window.location.protocol;
                    window.location.href =protocal+"//"+host +  '/mlogin?redirectUrl='+window.location.href
                }
            });
        }
    }
}
